package com.beyondsoft.sabg.edu.controller.dataanalysis;

import com.beyondsoft.sabg.edu.model.dto.BusinessLogicDto;
import com.beyondsoft.sabg.edu.model.vo.core.SimpleResponseVO;
import com.beyondsoft.sabg.edu.service.dataanalysis.BusinessLogicService;
import com.beyondsoft.sabg.edu.utils.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 业务逻辑控制器
 */
@RestController
@RequestMapping("/api/dataanalysis/business-logic")
@Slf4j
public class BusinessLogicController {

    @Autowired
    private BusinessLogicService businessLogicService;

    /**
     * 创建业务逻辑
     */
    @PostMapping
    public SimpleResponseVO createBusinessLogic(@Valid @RequestBody BusinessLogicDto businessLogicDto) {
        log.info("创建业务逻辑请求，描述：{}", businessLogicDto.getDescription());
        
        try {
            String result = businessLogicService.createBusinessLogic(businessLogicDto);
            return ResponseUtil.success().withData(result);
        } catch (Exception e) {
            log.error("创建业务逻辑失败", e);
            return ResponseUtil.failed("创建业务逻辑失败：" + e.getMessage());
        }
    }

    /**
     * 获取业务逻辑列表
     */
    @GetMapping
    public SimpleResponseVO listBusinessLogic(
            @RequestParam(required = false) String nextToken,
            @RequestParam(defaultValue = "10") Integer maxResults) {
        log.info("获取业务逻辑列表请求");
        
        try {
            Object result = businessLogicService.listBusinessLogic(nextToken, maxResults);
            return ResponseUtil.success().withData(result);
        } catch (Exception e) {
            log.error("获取业务逻辑列表失败", e);
            return ResponseUtil.failed("获取业务逻辑列表失败：" + e.getMessage());
        }
    }

    /**
     * 修改业务逻辑
     */
    @PutMapping
    public SimpleResponseVO updateBusinessLogic(@Valid @RequestBody BusinessLogicDto businessLogicDto) {
        log.info("修改业务逻辑请求，ID：{}", businessLogicDto.getBusinessLogicIdKey());
        
        try {
            String result = businessLogicService.updateBusinessLogic(businessLogicDto);
            return ResponseUtil.success().withData(result);
        } catch (Exception e) {
            log.error("修改业务逻辑失败", e);
            return ResponseUtil.failed("修改业务逻辑失败：" + e.getMessage());
        }
    }

    /**
     * 批量删除业务逻辑
     */
    @DeleteMapping("/batch")
    public SimpleResponseVO batchDeleteBusinessLogic(@RequestBody List<String> BusinessLogicIdKeys) {
        log.info("批量删除业务逻辑，数量：{}", BusinessLogicIdKeys.size());

        try {
            String result = businessLogicService.batchDeleteBusinessLogic(BusinessLogicIdKeys);
            return ResponseUtil.success().withData(result);
        } catch (Exception e) {
            log.error("批量删除业务逻辑失败", e);
            return ResponseUtil.failed("批量删除业务逻辑失败：" + e.getMessage());
        }
    }

} 